<template>
  <div class="traffic-chart"
    :style="{height: height}"
    :id="dId ? dId:'lineChart'"></div>
</template>
<script>
export default {
  name: 'LineChart',
  props: {
    chartData: {
      type: Array,
      default: () => [],
    },
    dId: {
      type: String,
      default: '',
    },
    height: {
      type: String,
      default: '220px',
    },
  },
  data() {
    return {
      myLineChart: null,
    }
  },
  watch: {
    chartData: {
      deep: true,
      immediate: true,
      handler(val) {
        if (val && JSON.stringify(val) !== '{}') {
          this.$nextTick(() => {
            this.lineChart()
          })
        }
      },
    },
  },
  mounted() {
    window.addEventListener('resize', this.resizeHandler)
  },
  methods: {
    lineChart() {
      this.myLineChart = this.$echarts.init(
        document.getElementById(`${this.dId ? this.dId : 'lineChart'}`)
      )

      var captions = []
      var values = []

      this.chartData.forEach((item) => {
        captions.push(item.date)
        values.push(item.value)
      })

      const y_min = Math.min(...values)
      const y_max = Math.max(...values)
      const y_echartMid = (y_max + y_min) / 2
      const y_echartMin = Math.round(y_echartMid - (y_echartMid - y_min) * 1.1)

      let that = this

      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'none',
          },
          position: function (point, params, dom, rect, size) {
            // 提示框位置
            let x = 0
            let y = 0
            if (point[0] + size.contentSize[0] + 10 > size.viewSize[0]) {
              x = point[0] - size.contentSize[0] - 10
            } else {
              x = point[0] + 10
            }
            if (point[1] + size.contentSize[1] + 10 > size.viewSize[1]) {
              y = point[1] - size.contentSize[1] - 10
            } else {
              y = point[1] + 10
            }
            return [x, y]
          },
          formatter: (params) => {
            const { name, data } = params[0]
            return `
            <div style="font-size: 14px;font-family: Source Han Sans CN-Medium;font-weight: 500;color: #FFFFFF;">${name}：${parseInt(data).toLocaleString()}</div>`
          },
          extraCssText:
            'opacity: 0.8;background-color:#050F1B;padding:16px;box-shadow: 1px 6px 15px 1px rgba(0,0,0,0.13);border-radius: 4px;filter: blur(undefinedpx);border:none;',
        },
        grid: {
          top: '10%', //上边距
          right: '20', //右边距
          left: '20', //左边距
          bottom: '10%', //下边距
          containLabel: true,
        },
        xAxis: {
          type: 'category',
          data: captions,
          axisTick: {
            show: false, //隐藏X轴刻度
          },
          axisLine: {
            lineStyle: {
              color: '#CCCCCC',
            },
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: 'rgba(0,0,0,0.65)',
              fontSize: 14,
              fontFamily: 'Source Han Sans CN-Regular',
            },
          },
        },
        yAxis: [
          {
            type: 'value',
            name: '',
            min: y_echartMin,
            nameTextStyle: {
              color: 'rgba(0,0,0,0.65)',
              fontSize: 14,
              fontFamily: 'Source Han Sans CN-Regular',
              align: 'left',
              verticalAlign: 'center',
            },
            axisLabel: {
              color: 'rgba(0,0,0,0.65)',
              textStyle: {
                fontSize: 14,
              },
              formatter: function (value) {
                return that.$vReset(value)
              },
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: 'rgba(223, 223, 223, 1)',
              },
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              lineStyle: {
                color: 'rgba(223, 223, 223, 1)',
                type: 'dashed',
              },
            },
          },
        ],
        series: [
          {
            type: 'line',
            data: values,
            smooth: true,
            symbolSize: 8, //标记的大小
            areaStyle: {
              opacity: 0.3,
              color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#7150E6',
                },
                {
                  offset: 1,
                  color: '#FFFFFF',
                },
              ]),
            },
            lineStyle: {
              color: '#7150E6',
              width: 2,
              /* shadowColor: 'rgba(113, 80, 230, 0.5)', //设置折线阴影
              shadowBlur: 5,
              shadowOffsetY: 9, */
            },
            itemStyle: {
              //折线拐点标志的样式
              color: '#7150E6',
              borderColor: '#7150E6',
              borderWidth: 1,
            },
            emphasis: {
              scale: 1.5,
            },
          },
        ],
      }
      this.myLineChart.setOption(option)
    },
    resizeHandler() {
      if (this.myLineChart) {
        this.myLineChart.resize()
      }
    },
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeHandler)
    if (!this.myLineChart) {
      return
    }
    this.myLineChart.dispose()
    this.myLineChart = null
  },
}
</script>
<style lang="scss" scoped>
.traffic-chart {
  margin-top: 30px;
}
</style>
